import time

from log import SnailLog
from schemas import ExecuteResult
from snail import SnailJobError, job


@job("testJobExecutor")
def test_job_executor(args: str) -> ExecuteResult:
    SnailLog.REMOTE.info("sync job1: " + args)

    # 执行一个超过40秒的任务，如果中间第30秒可以正常发送心跳，说明任务执行不阻塞
    for i in range(40):
        SnailLog.REMOTE.info(f"loop {i}")
        time.sleep(1)

    SnailLog.REMOTE.info("sync job1 done")
    return ExecuteResult.success()


@job("testJobExecutorFailed")
def test_job_executor_failed(args: str):
    SnailLog.LOCAL.info("testJobExecutorFailed, SnailJobError raised")
    raise SnailJobError("这是故意抛出的异常")
